<template>
  <!-- 分配宿舍情况 -->
  <div class="app-container">
    <el-row>
      <el-form ref="form" :model="form" :inline="true" label-width="65px">
        <el-form-item label="院系">
          <!-- <el-cascader
            clearable
            v-model="form.deptId"
            :options="deptOptions"
            :props="{ value: 'id',currentLabels: 'label', checkStrictly: true }"
            @change="handleChange"
            style="width:100%"
          ></el-cascader> -->
          <el-select v-model="form.deptId" placeholder="请选择" clearable class="elSelect" @change="handleChange"  style="width: 100%">
                <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId"></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="年级">
          <el-select
            v-model="form.gradeId"
            placeholder="请选择"
            @change="changeGrade"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="item in gradeDate"
              :key="item.VALUE"
              :label="item.TITILE"
              :value="item.VALUE"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业">
          <el-select
            v-model="form.specialityId"
            placeholder="请选择"
            clearable
            class="elSelect"
            @change="changeGrade"
            style="width:100%"
          >
            <el-option
              v-for="item in profession"
              :key="item.VALUE"
              :label="item.TITILE"
              :value="item.VALUE"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级">
          <el-select
            v-model="form.classId"
            placeholder="请选择"
            clearable
            class="elSelect"
            style="width:100%"
          >
            <el-option
              v-for="item in classList"
              :key="item.VALUE"
              :label="item.TITLE"
              :value="item.VALUE"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起始时间">
          <el-date-picker
            v-model="form.startDate"
            type="date"
            clearable
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 94%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="截至时间">
          <el-date-picker
            v-model="form.endDate"
            type="date"
            clearable
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 94%"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <div style="float:right;">
            <el-button type="primary" size="mini" @click="totality">统计</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 点击统计时候数据才请求 1 -->
    <el-table :data="tableData.data" style="width: 100%" v-if="tableDisable" key="ket">
      <el-table-column prop="dates" label="日期" align="center"></el-table-column>
      <el-table-column
        v-for="(v,i) in tableData.tableHeader"
        :label="v.feeTypeName"
        align="center"
        :key="i"
      >
        <el-table-column :prop="'stuCount_'+v.feeType+'_0'" label="男" align="center">
          <!-- {{ 'stuCount'+v.feeType+'0' }} -->
        </el-table-column>
        <el-table-column :prop="'stuCount_'+v.feeType+'_1'" label="女" align="center">
          <!-- {{ 'stuCount'+v.feeType+'1' }} -->
        </el-table-column>
      </el-table-column>
    </el-table>
  </div>
</template>
  
  <script>
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
import { deptTreeSelect } from "@/api/system/user";
import { getClassSelect } from "@/api/information/classInfor";
import { getGradeSelect } from "@/api/information/gradeInfor";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { getAssignRoomStatistics } from "@/api/apartment/statistics";
import {
  collegeSelect
} from "@/api/information/studentInfor"

export default {
  components: { searchForm },
  data() {
    return {
      form: {},

      tableData: {
        tableHeader: [],
        data: [ ]
      },
      obj: {},
      labelList: [],
      classList: [],
      profession: [],
      gradeDate: [],
      deptOptions: [],
      tableDisable: false
    };
  },
  created() {
    this.department();
    this.getGradeInfor();
  },
  methods: {
    totality() {
      this.tableData.data = [];
      getAssignRoomStatistics(this.form).then(res => {
        if(res.data.length<1){
          this.tableDisable = false
          this.$modal.msgWarning("统计条件暂时没有数据，请重新统计");
        }else{
          this.tableDisable = true
        }
        // console.log(res.data, 4555555555555);
        // this.tableData = res.data;
        const result = res.data.reduce((total, cur) => {
          const hasValue = total.findIndex(current => {
            return current.feeType === cur.feeType;
          });
          if (hasValue === -1) {
            cur[`stuCount_${cur.feeType}`] = cur.stuCount;
            cur[`sex_${cur.feeType}`] = cur.stuCount;
            total.push(cur);
          } else {
            const tmp = total[hasValue];
            // total[hasValue][`stuCount_${tmp.feeType}`] = tmp.stuCount;
            // total[hasValue][`sex_${tmp.feeType}`] = tmp.sex;
            total[hasValue][`stuCountMan`] = cur.stuCount;
            total[hasValue][`sexMan`] = cur.sex;
          }
          return total;
        }, []);
        // console.log(result, "11111111111111");
        this.tableData.tableHeader = result;
        for (let i = 0; i < result.length; i++) {
          switch (i) {
            case 0:
              (this.obj[`stuCount_${result[0].feeType}_0`] =
                result[0].stuCount),
                (this.obj[`stuCount_${result[0].feeType}_1`] =
                  result[0].stuCountMan);
              break;
            case 1:
              (this.obj[`stuCount_${result[1].feeType}_0`] =
                result[1].stuCount),
                (this.obj[`stuCount_${result[1].feeType}_1`] =
                  result[1].stuCountMan);
              break;
            case 2:
              (this.obj[`stuCount_${result[2].feeType}_0`] =
                result[2].stuCount),
                (this.obj[`stuCount_${result[2].feeType}_1`] =
                  result[2].stuCountMan);
              break;
            case 3:
              (this.obj[`stuCount_${result[3].feeType}_0`] =
                result[3].stuCount),
                (this.obj[`stuCount_${result[3].feeType}_1`] =
                  result[3].stuCountMan);
              break;
            case 4:
              (this.obj[`stuCount_${result[4].feeType}_0`] =
                result[4].stuCount),
                (this.obj[`stuCount_${result[4].feeType}_1`] =
                  result[4].stuCountMan);
              break;
            case 5:
              (this.obj[`stuCount_${result[5].feeType}_0`] =
                result[5].stuCount),
                (this.obj[`stuCount_${result[5].feeType}_1`] =
                  result[5].stuCountMan);
              break;
            case 6:
              (this.obj[`stuCount_${result[6].feeType}_0`] =
                result[6].stuCount),
                (this.obj[`stuCount_${result[6].feeType}_1`] =
                  result[6].stuCountMan);
              break;
            case 7:
              (this.obj[`stuCount_${result[7].feeType}_0`] =
                result[7].stuCount),
                (this.obj[`stuCount_${result[7].feeType}_1`] =
                  result[7].stuCountMan);
              break;
            case 8:
              (this.obj[`stuCount_${result[8].feeType}_0`] =
                result[8].stuCount),
                (this.obj[`stuCount_${result[8].feeType}_1`] =
                  result[8].stuCountMan);
              break;
            case 9:
              (this.obj[`stuCount_${result[9].feeType}_0`] =
                result[9].stuCount),
                (this.obj[`stuCount_${result[9].feeType}_1`] =
                  result[9].stuCountMan);
              break;
            case 10:
              (this.obj[`stuCount_${result[10].feeType}_0`] =
                result[10].stuCount),
                (this.obj[`stuCount_${result[10].feeType}_1`] =
                  result[10].stuCountMan);
              break;
          }
        }
  
       
        this.tableData.data.push(this.obj);
       
          this.tableData.data.forEach(element => {
            if(this.form.startDate || this.form.endDate){
              element.dates = this.form.startDate + '——' + this.form.endDate
            }else {
              element.dates = '暂无数据'
            }
          });
        
      });
      console.log( this.tableData.data)
    },
    reset() {
      this.form = {}
      this.totality()
    },
    handleChange(value) {
      this.form.specialityId = "";
      this.form.deptId = value;
      this.getSpeciality();
      // if (typeof value == "object") {
      //   this.form.deptId = value[value.length - 1];
      //   // this.queryParams.deptId = value[value.length - 1];
      //   this.getSpeciality();
      // } else if (typeof value == "number") {
      //   this.form.deptId = value;
      //   this.getSpeciality();
      // } else if (value == undefined) {
      //   this.form.specialityId = "";
      // }
    },
    async getSpeciality() {
      if (this.form.deptId) {
        await getSpecialityByDeptId({ deptId: this.form.deptId }).then(res => {
          if (res.code == "200") {
            this.profession = res.data;
          } else {
            this.profession = [];
          }
        });
      } else {
        this.profession = [];
      }
    },
    getGradeInfor() {
      getGradeSelect().then(response => {
        console.log(response, "Student");
        this.gradeDate = response.data;
      });
    },
    getClassList() {
      getClassSelect({
        gradeId: this.form.gradeId,
        specialtyId: this.form.specialityId
      }).then(res => {
        this.classList = res.data;
      });
    },
    // 获取部门信息
    department() {
      // await deptTreeSelect().then(res => {
      //   this.deptOptions = res.data;
      // });
      collegeSelect().then(res => {
        this.deptOptions = res.data;
      });
    },
    // 获取班级拉下树
    changeGrade(value) {
      console.log(value);
      if (this.form.specialityId && this.form.gradeId) {
        this.getClassList();
      } else {
        if (!value) {
          this.classList = [];
          this.form.classId = "";
        }
      }
    }
  }
};
</script>